package com.dayuanit.dy15.atm.util;

import java.util.Random;

public class CardUtil {

    private static final String[] numbers = {"0", "1", "2", "3", "4", "5", "6","7", "8", "9"};

    private static final int CARD_LENG = 5;

    public static String createCardNum() {
        //StringBuilder 不是线程安全的 但是性能高
        StringBuilder cardNum = new StringBuilder();

        //StringBuffer线程安全，性能低
//        StringBuffer sx = new StringBuffer();

        Random random = new Random();

        for (int i=0; i<CARD_LENG; i++) {
            int index = random.nextInt(10);
//            cardNum += numbers[index];//不要使用+号拼接字符串，会极大的影响系统性能
            cardNum.append(numbers[index]);//推荐使用stringbuilder拼接字符串
        }

        return cardNum.toString();
    }

    public static String formatNum(String cardNum) {
        StringBuilder sx = new StringBuilder();

        for (int i=0; i< CARD_LENG; i++) {
            if (i !=0 && i!= CARD_LENG - 1) {
                sx.append("*");
            } else {
                sx.append(cardNum.charAt(i));
            }
        }
        return sx.toString();
    }

    public static void main(String[] args) {
//        for (int i=0; i<10; i++) {
//            System.out.println(CardUtil.createCardNum());
//        }

        System.out.println(formatNum("08765"));

    }
}
